package session.stateless;

import entity.Customer;
import entity.LineItem;
import entity.Product;
import entity.SaleInquiry;
import entity.SaleLead;
import entity.SaleQuotation;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class SaleInquiryBean implements SaleInquiryBeanLocal {
    @PersistenceContext()
    EntityManager em;
    
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    
    public LineItem createLineItem(Product product, Long quantity) {
        LineItem lineItem = new LineItem();

        lineItem.setLineItemQuantity(quantity);
        lineItem.setProduct(product);

        em.persist(lineItem);
        em.flush();
        System.out.println("SI Session"+lineItem.getLineItemQuantity());
        return lineItem;
    }
    
   public LineItem createLineItem(Product product,Long quantity, Double finalPrice) {
            LineItem lineItem = new LineItem();
            
            lineItem.setLineItemQuantity(quantity);
            lineItem.setFinalPrice(finalPrice);
            lineItem.setProduct(product);
            
            em.persist(lineItem);
            em.flush();
            
            return lineItem;
    }
    
    public void deleteLineItem(long lineItemId){
        LineItem lineItem = em.find(LineItem.class, lineItemId);
        if(lineItem==null){
            System.out.println("Line Item does not exist!");
        }
        else{
            lineItem.setProduct(null);
            em.remove(lineItem);
            em.flush();
        }
    }

    public Long createSaleInquiry(String registrationNumber,List<LineItem> lineItems) {
        SaleInquiry saleInquiry = new SaleInquiry();
        
        SaleLead saleLead =em.find(SaleLead.class, registrationNumber);
        if (saleLead == null)
        {
            System.out.println("Sale lead does not exist!");   
            return null;
        }
        else
        {
            saleInquiry.setSaleLead(saleLead);               
   
            saleInquiry.setLineItems(lineItems);
            
            Date date = new Date();
            saleInquiry.setSaleInquiryDate(dateFormat.format(date));
            em.persist(saleInquiry);
            em.flush();
            //add the new inquiry to the sale lead's list of sale inquiries
            List<SaleInquiry> saleInquiries = new ArrayList<SaleInquiry>();
            saleInquiries = saleLead.getSaleInquiries();
            saleInquiries.add(saleInquiry);
            saleLead.setSaleInquiries(saleInquiries);
            em.persist(saleLead);
            em.flush();
            
            return saleInquiry.getId();
        }
    }

    public void updateSaleInquiry(Long saleInquiryId, List<LineItem> lineItems) {
        SaleInquiry saleInquiry =em.find(SaleInquiry.class, saleInquiryId);
        
        if (saleInquiry ==null)
        {
            System.out.println("Sale Inquiry does not exist.");            
        }
        else
        {
            Object[] objectList = saleInquiry.getLineItems().toArray();
            saleInquiry.setLineItems(null);
            ArrayList<LineItem> tempLineItems = new ArrayList<LineItem>();
            LineItem lineItem = new LineItem();
            for(int i=0; i<objectList.length;i++){
                lineItem = (LineItem) objectList[i];
                deleteLineItem(lineItem.getLineItemId());
            }
            
            objectList = lineItems.toArray();
            for(int i=0; i<objectList.length;i++){
                lineItem = (LineItem) objectList[i];
                tempLineItems.add(createLineItem(lineItem.getProduct(),lineItem.getLineItemQuantity()));
            }
            
            saleInquiry.setLineItems(tempLineItems);
            em.persist(saleInquiry);
            em.flush();
        }
    }

    public void deleteSaleInquiry(Long saleInquiryId) {
        SaleInquiry saleInquiry = em.find(SaleInquiry.class, saleInquiryId);
        
        if (saleInquiry ==null)
        {
            System.out.println("Sale Inquiry does not exist");
        }
        else
        {
            List<SaleInquiry> newSaleInquiries = new ArrayList<SaleInquiry>();
            newSaleInquiries = saleInquiry.getSaleLead().getSaleInquiries();
            newSaleInquiries.remove(saleInquiry);
            
            saleInquiry.getSaleLead().setSaleInquiries(newSaleInquiries);
            
            Object[] objectList = saleInquiry.getLineItems().toArray();
            saleInquiry.setLineItems(null);
            LineItem lineItem = new LineItem();
            for(int i=0; i<objectList.length;i++){
                lineItem = (LineItem) objectList[i];
                deleteLineItem(lineItem.getLineItemId());
            }
            
            em.remove(saleInquiry);
            em.flush();
        }
    }

    public List<SaleInquiry> getSaleInquiries() {
        Query q1=em.createQuery("SELECT c FROM SaleInquiry c");
        List<SaleInquiry> saleInquiryList = q1.getResultList();
        Query q2 = em.createQuery("SELECT c FROM SaleQuotation c");
        List<SaleQuotation> saleQuotationList = q2.getResultList();
        
        for(Object o: saleQuotationList){
            SaleInquiry temp = (SaleInquiry) o;
            saleInquiryList.remove(temp);
        }
        
        return saleInquiryList;
    }
    
    public List<SaleInquiry> getSaleInquiriesBySaleLead(String registrationNumber) {
        SaleLead saleLead=em.find(SaleLead.class, registrationNumber);
        
        return (List<SaleInquiry>) saleLead.getSaleInquiries();
    }

    public List<SaleInquiry> getSaleInquiriesByCustomer(String registrationNumber) {
        Customer customer =em.find(Customer.class, registrationNumber);
        
        return customer.getSaleInquiries();
    }
    
    public List<Product> getProduct() {
        Query q = em.createQuery("SELECT c FROM Product c");
        
        return q.getResultList();
    }


}
